//
//  WKViewController.m
//  JiangHu
//
//  Created by GT mac on 16/5/9.
//  Copyright © 2016年 GT mac. All rights reserved.
//

#import "WKViewController.h"
#import "commonfile.h"

@interface WKViewController ()
@property (nonatomic, strong) WKWebView *webView;
@end

@implementation WKViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    config.preferences = [[WKPreferences alloc] init];
    config.preferences.minimumFontSize = 10;
    config.preferences.javaScriptEnabled = YES;
    config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
    config.processPool = [[WKProcessPool alloc] init];
    config.userContentController = [[WKUserContentController alloc] init];
    [config.userContentController addScriptMessageHandler:self name:@"JHJSB"];
    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) configuration:config];
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:MAIN_URL]]];
}

#pragma mark -- WKNavigationDelegate
//页面开始加载时
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    NSLog(@"didStartProvisionalNavigation");
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

//内容开始返回时
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
    NSLog(@"didCommitNavigation");
}

//页面加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    NSLog(@"didFailProvisionalNavigation");
}

//页面加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    NSLog(@"didFinishNavigation");
    //    [self resetControl];
    if (webView.title.length > 0) {
        self.title = webView.title;
    }
}

#pragma mark - WKUIDelegate
- (void)webViewDidClose:(WKWebView *)webView {
    //    NSLog(@"%s", __FUNCTION__);
}

- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
    NSLog(@"%s", __FUNCTION__);
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler();
    }]];
    
    [self presentViewController:alert animated:YES completion:NULL];
    NSLog(@"%@", message);
    
}

- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler {
    NSLog(@"%s", __FUNCTION__);
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"confirm" message:@"JS调用confirm 1" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(YES);
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(NO);
    }]];
    [self presentViewController:alert animated:YES completion:NULL];
    
    NSLog(@"%@", message);
}

- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler {
    NSLog(@"%s", __FUNCTION__);
    
    NSLog(@"%@", prompt);
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"textinput" message:@"JS调用输入框 1" preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.textColor = [UIColor redColor];
    }];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler([[alert.textFields lastObject] text]);
    }]];
    
    [self presentViewController:alert animated:YES completion:NULL];
}

#pragma mark -- WKScriptMessageHandler
//JS调用OC
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    NSLog(@"JS调用OC");
    NSLog(@"%@", message.body);
    NSDictionary *dic = message.body;
    NSString *str = [dic objectForKey:@"body"];
    //    NSString *str = [NSString stringWithFormat:@"%@",message.body];
//    [self alipayPay:str];
    //    if ([message.name isEqualToString:@"JHJSB"]) {
    //        NSString *strUrl = [str stringByReplacingOccurrencesOfString:@"\\" withString:@""];
    //        NSLog(@"%@", strUrl);
    //        [self alipayPay:str];
    //    }
    
        //去{}
        NSString *str1 = [str stringByReplacingOccurrencesOfString:@"{" withString:@""];
        NSString *str2 = [str1 stringByReplacingOccurrencesOfString:@"}" withString:@""];
        //去""
        NSString *str3 = [str2 stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        NSLog(@"str3 == %@",str3);
    
        int lastLocation = 0;
        NSMutableArray *array = [[NSMutableArray alloc] init];
        for (int index = 0; index < 6; index ++) {
            NSRange range = [str3 rangeOfString:@","];
            NSLog(@"rang:%@",NSStringFromRange(range));
            NSString *str4 = [str3 substringWithRange:NSMakeRange(lastLocation, range.location)];
            NSLog(@"截取的值为：%@",str4);
            NSRange range2 = [str4 rangeOfString:@":"];
            NSLog(@"rang2:%@",NSStringFromRange(range2));
            NSString *str5 = [str4 substringWithRange:NSMakeRange(range2.location+1, range.location-range2.location-1)];
            [array addObject:str5];
            str3 = [str3 substringWithRange:NSMakeRange(range.location+1, str3.length-range.location-1)];
            NSLog(@"array22 == %@",array);
            NSLog(@"str3 == %@",str3);
        }
        NSLog(@"array == %@",array);
//        [self WXPayRmbpartnerId:[array objectAtIndex:0] prepayId:[array objectAtIndex:1] packageValue:[array objectAtIndex:2] nonceStr:[array objectAtIndex:3] timeStamp:[array objectAtIndex:4] sign:[array objectAtIndex:5]];
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
